package com.boge.controller.user;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.annotation.Resource;
import javax.json.JsonArray;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boge.model.dao.mappers.UserMapper;
import com.boge.model.entity.User;
import com.boge.service.UserService;
import com.boge.utils.myutils.AjaxResult;
import com.boge.utils.myutils.ExportTableUtil;

@Controller
@RequestMapping("/user2")
public class UserController2 {

	@Resource
	UserService userService;
	
	@RequestMapping("/getUserListByProp")
	@ResponseBody
	public AjaxResult getUserListByProp(Integer pageindex, Integer pageSize, HttpServletRequest request) {
		System.out.println("pageindex:" + pageindex);
		System.out.println("pageSize:" + pageSize);
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			System.out.println("nextElement:" + enumeration.nextElement());
		}
		System.out.println("formData:" + request.getParameter("formData"));
		
		int page = Integer.parseInt(request.getParameter("pageindex"));
		int rows = Integer.parseInt(request.getParameter("pageSize"));
		JSONObject formJson = JSONObject.parseObject(request.getParameter("formData"));
		System.out.println("userName:" + formJson.getString("userName"));
		System.out.println("userSex:" + formJson.getString("userSex"));
		
		String userName = formJson.getString("userName");
		String userSex = formJson.getString("userSex");
		
		List<User> users = userService.getUserListByProp(page, rows, userName, userSex);
		Integer totalNum = userService.getTotalCountFromUser(userName, userSex);
		System.out.println("users:" + users);
		Map<String, Object> map = new HashMap<>();
		map.put("users", users);
		map.put("totalNum", totalNum);
		return new AjaxResult(map);
	}
	
	@RequestMapping("/exportSelectedData")
	public void exportSelectedData(HttpServletRequest request, HttpServletResponse response) {
		//json字符串
		String jsonStr = request.getParameter("json");
		//表格表头
		String sheaders = request.getParameter("headers");
		JSONArray jsonArray = JSONArray.parseArray(jsonStr);
		JSONArray jsonArray2 = new JSONArray();
		System.out.println("jsonArray.size():" + jsonArray.size());
		int size = jsonArray.size();
		System.out.println("size:" + size);
		for (int i = 0; i < size; i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			System.out.println("jsonObject:" + jsonObject);
			transferUserToClient(jsonArray2, jsonObject);
		}
		String title = "selected user data";
		new ExportTableUtil().export(jsonArray2.toJSONString(),sheaders,title, response, request);
	}
	
	@RequestMapping("/exportCurrentData")
	public void exportCurrentData(HttpServletRequest request, HttpServletResponse response) {
		//json字符串
		String jsonStr = request.getParameter("json");
		//表格表头
		String sheaders = request.getParameter("headers");
		//表格标题名
//		String title = request.getParameter("fileName");
		String title = "current page user data";
		new ExportTableUtil().export(jsonStr,sheaders,title, response, request);
	}
	
	@RequestMapping("/exportAllData")
	public void exportAllData(HttpServletRequest request, HttpServletResponse response) {
		//表格表头
		String sheaders = request.getParameter("headers");
		//查询所有数据并转为json格式
		JSONArray jsonArray = new JSONArray();
		List<User> userList = userService.findAllUser();
		for (User user : userList) {
			JSONObject jsonObject = (JSONObject) JSONObject.toJSON(user);
			System.out.println("jsonObject:" + jsonObject);
			transferUserToClient(jsonArray, jsonObject);
		}
		String title = "all user data";
		new ExportTableUtil().export(jsonArray.toJSONString(),sheaders,title, response, request);
		
	}
	
	/**
	 * 导出的excel将数据库获取的数据转换成前端的形式
	 * @param jsonObject
	 */
	private void transferUserToClient(JSONArray jsonArray, JSONObject jsonObject) {
		//获取到的是set，需转为list
		List<String> keyList = new ArrayList(jsonObject.keySet());
		for (int i = 0; i < keyList.size(); i++) {
			//将数据库获取的key转换为前端的列名
			String key = keyList.get(i);
			if (key.equals("userName")) {
				jsonObject.put("用户名", jsonObject.get(key));
			}
			if (key.equals("userPassword")) {
				jsonObject.put("密码", jsonObject.get(key));
			}
			if (key.equals("userSex")) {
				//将值转换为前端形式
				Integer sex = (Integer) jsonObject.get(key);
				String sexString = null;;
				if (sex == 0) {
					sexString = "男";
				} else if (sex == 1) {
					sexString = "女";
				} else {
					sexString = "未知";
				}
				jsonObject.put("性别", sexString);
			}
			if (key.equals("userMail")) {
				jsonObject.put("邮箱", jsonObject.get(key));
			}
			if (key.equals("userPhone")) {
				jsonObject.put("手机号", jsonObject.get(key));
			}
		}
		jsonArray.add(jsonObject);
	}
	
}
